#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @Time		:2018/3/5 10:17
# @Author	:RNone
from api.common_bulk_insert import common_bulk_insert
from api.common_modify import common_modify
from api.common_search import common_search
import logging
from urllib.parse import quote_plus,unquote_plus


class http_data(common_modify, common_search, common_bulk_insert):
	def __search_data(self, caseDict):
		'''查询数据'''
		# 准备查询数据
		for i in caseDict['params']['fields']:
			if i != 'summary' and caseDict['params']['fields'][i]:
				keywords = caseDict['params']['fields'][i]
				searchKey = i
		searchData = caseDict.copy()
		searchData['params'] = {'from': 0, 'size': 10, 'conditions': [], 'sort': []}
		searchData['keywords'] = keywords
		# 查询需要修改的信息
		# 通过方法关键字判断需要使用的查询方法
		func = getattr(
			self,
			caseDict['function'].split('update')[0].split('delete')[0].split('add')[0] + 'search')
		searchDatas = func(searchData)
		searchDatas = searchDatas['response']['data']
		for i in searchDatas:
			if i[searchKey] == keywords:
				logging.debug('数据信息为：%s'%(i))
				searchData = i
				break
		else:
			logging.debug('没有查询到该数据')
			searchData = None
		return searchData

	def __host_track_modify(self, caseDict):
		'''host关注修改'''
		caseDict['params']['indexName'] = 'http-track'
		caseDict['params']['module'] = 10
		caseDict['params']['key_field'] = 'host'
		caseDict['params']['fields']['imei'] = ''
		caseDict['params']['fields']['create_time'] = ''
		caseDict = self.test_modify_data(caseDict)
		return caseDict

	def __object_track_modify(self, caseDict):
		'''对象关注修改'''
		caseDict['params']['indexName'] = 'http-object'
		caseDict['params']['module'] = 11
		caseDict['params']['key_field'] = 'imei'
		caseDict['params']['fields']['host'] = ''
		caseDict['params']['fields']['use_count'] = ''
		caseDict['params']['fields']['create_time'] = ''
		caseDict = self.test_modify_data(caseDict)
		return caseDict

	def __url_track_modify(self, caseDict):
		'''对象关注修改'''
		caseDict['params']['indexName'] = 'http-url-track'
		caseDict['params']['module'] = 12
		caseDict['params']['key_field'] = 'key_word'
		caseDict['params']['fields']['imei'] = ''
		caseDict['params']['fields']['create_time'] = ''
		caseDict = self.test_modify_data(caseDict)
		return caseDict

	def __http_white_modify(self, caseDict):
		'''对象关注修改'''
		caseDict['params']['indexName'] = 'http-white'
		caseDict['params']['module'] = 14
		caseDict['params']['key_field'] = 'host'
		caseDict['params']['fields']['create_time'] = ''
		caseDict = self.test_modify_data(caseDict)
		return caseDict

	def test_url_search(self, caseDict):
		'''url数据查询测试'''
		caseDict['params']['indexName'] = 'http-result'
		caseDict['params']['module'] = 8
		caseDict = self.test_search_data(caseDict)
		return caseDict

	def test_post_search(self, caseDict):
		'''post数据查询测试'''
		caseDict['params']['indexName'] = 'http-post-result'
		caseDict['params']['module'] = 9
		caseDict = self.test_search_data(caseDict)
		return caseDict

	def test_host_track_search(self, caseDict):
		'''host关注查询测试'''
		caseDict['params']['indexName'] = 'http-track'
		caseDict['params']['module'] = 10
		caseDict = self.test_search_data(caseDict)
		return caseDict

	def test_object_track_search(self, caseDict):
		'''对象关注查询测试'''
		caseDict['params']['indexName'] = 'http-object'
		caseDict['params']['module'] = 11
		caseDict = self.test_search_data(caseDict)
		return caseDict

	def test_url_track_search(self, caseDict):
		'''url关注查询测试'''
		caseDict['params']['indexName'] = 'http-url-track'
		caseDict['params']['module'] = 12
		caseDict = self.test_search_data(caseDict)
		return caseDict

	def test_host_ranking_search(self, caseDict):
		'''host排名查询测试'''
		caseDict['params']['indexName'] = 'http-host-ranking'
		caseDict['params']['module'] = 13
		caseDict = self.test_search_data(caseDict)
		return caseDict

	def test_http_white_search(self, caseDict):
		'''http白名单查询测试'''
		caseDict['params']['indexName'] = 'http-white'
		caseDict['params']['module'] = 14
		caseDict = self.test_search_data(caseDict)
		return caseDict

	def test_http_track_bulk_insert(self, caseDict):
		'''批量文件导入host关注信息'''
		caseDict['params'] = dict(
			{
				"indexName": 'http-track',
				"key_field": 'host',
				"defultField": ('create_time', 'imei'),
				"module": 10
			}
			, **caseDict['params'])
		caseDict = self.test_bulk_insert_data(caseDict)
		return caseDict

	def test_object_track_bulk_insert(self, caseDict):
		'''批量导入对象关注信息'''
		caseDict['params'] = dict(
			{
				"indexName": 'http-object',
				"key_field": 'imei',
				"defultField": ('host', 'create_time', 'use_count'),
				"module": 11
			}
			, **caseDict['params'])
		caseDict = self.test_bulk_insert_data(caseDict)
		return caseDict

	def test_url_track_bulk_insert(self, caseDict):
		'''批量导入对象关注信息'''
		caseDict['params'] = dict(
			{
				"indexName": 'http-url-track',
				"key_field": 'key_word',
				"defultField": ('imei', 'create_time'),
				"module": 12
			}
			, **caseDict['params'])
		caseDict = self.test_bulk_insert_data(caseDict)
		return caseDict

	def test_http_white_bulk_insert(self, caseDict):
		'''批量导入对象关注信息'''
		caseDict['params'] = dict(
			{
				"indexName": 'http-white',
				"key_field": 'host',
				"defultField": ('create_time'),
				"module": 14
			}
			, **caseDict['params'])
		caseDict = self.test_bulk_insert_data(caseDict)
		return caseDict

	def test_host_track_add(self, caseDict, tearDown=False):
		'''host关注新增测试'''
		if 'tearDown' in caseDict['params']:
			tearDown = caseDict['params']['tearDown']
			del caseDict['params']['tearDown']
		# 删除创建的数据
		if tearDown:
			self.test_host_track_delete(caseDict)
		caseDict['params']['op'] = 'add'
		caseDict = self.__host_track_modify(caseDict)
		return caseDict

	def test_object_track_add(self, caseDict, tearDown=False):
		'''对象关注新增测试'''
		if 'tearDown' in caseDict['params']:
			tearDown = caseDict['params']['tearDown']
			del caseDict['params']['tearDown']
		# 删除创建的数据
		if tearDown:
			self.test_object_track_delete(caseDict)
		caseDict['params']['op'] = 'add'
		caseDict = self.__object_track_modify(caseDict)
		return caseDict

	def test_url_track_add(self, caseDict, tearDown=False):
		'''对象关注新增测试'''
		if 'tearDown' in caseDict['params']:
			tearDown = caseDict['params']['tearDown']
			del caseDict['params']['tearDown']
		# 删除创建的数据
		if tearDown:
			self.test_url_track_delete(caseDict)
		caseDict['params']['op'] = 'add'
		caseDict = self.__url_track_modify(caseDict)
		return caseDict

	def test_http_white_add(self, caseDict, tearDown=False):
		'''对象关注新增测试'''
		if 'tearDown' in caseDict['params']:
			tearDown = caseDict['params']['tearDown']
			del caseDict['params']['tearDown']
		# 删除创建的数据
		if tearDown:
			self.test_http_white_delete(caseDict)
		caseDict['params']['op'] = 'add'
		caseDict = self.__http_white_modify(caseDict)
		return caseDict

	def test_host_track_delete(self, caseDict, setUp=False):
		'''host关注删除测试'''
		# 创造需要删除的数据
		if 'setUp' in caseDict['params']:
			setUp = caseDict['params']['setUp']
			del caseDict['params']['setUp']
		if setUp:
			self.test_host_track_add(caseDict)
		# 查询需要删除host数据的id
		data = self.__search_data(caseDict)
		if not data:
			data = {'indexid': ''}
		# 准备测试数据
		caseDict['params']['op'] = 'delete'
		caseDict['params']['ids'] = data['indexid']
		caseDict['params']['fields']['summary'] = data['summary']
		# 执行测试
		caseDict = self.__host_track_modify(caseDict)
		return caseDict

	def test_object_track_delete(self, caseDict, setUp=False):
		'''对象关注删除测试'''
		# 创造需要删除的数据
		if 'setUp' in caseDict['params']:
			setUp = caseDict['params']['setUp']
			del caseDict['params']['setUp']
		if setUp:
			self.test_object_track_add(caseDict)
		# 查询需要删除host数据的id
		data = self.__search_data(caseDict)
		if not data:
			data = {'indexid': ''}
		# 准备测试数据
		caseDict['params']['op'] = 'delete'
		caseDict['params']['ids'] = data['indexid']
		caseDict['params']['fields']['summary'] = data['summary']

		# 执行测试
		caseDict = self.__object_track_modify(caseDict)
		return caseDict

	def test_url_track_delete(self, caseDict, setUp=False):
		'''对象关注删除测试'''
		# 创造需要删除的数据
		if 'setUp' in caseDict['params']:
			setUp = caseDict['params']['setUp']
			del caseDict['params']['setUp']
		if setUp:
			self.test_url_track_add(caseDict)
		# 查询需要删除host数据的id
		data = self.__search_data(caseDict)
		if not data:
			data = {'indexid': ''}
		# 准备测试数据
		caseDict['params']['op'] = 'delete'
		caseDict['params']['ids'] = data['indexid']
		caseDict['params']['fields']['summary'] = data['summary']

		# 执行测试
		caseDict = self.__url_track_modify(caseDict)
		return caseDict

	def test_http_white_delete(self, caseDict, setUp=False):
		'''对象关注删除测试'''
		# 创造需要删除的数据
		if 'setUp' in caseDict['params']:
			setUp = caseDict['params']['setUp']
			del caseDict['params']['setUp']
		if setUp:
			self.test_http_white_add(caseDict)
		# 查询需要删除host数据的id
		data = self.__search_data(caseDict)
		if not data:
			data = {'indexid': ''}
		# 准备测试数据
		caseDict['params']['op'] = 'delete'
		caseDict['params']['ids'] = data['indexid']
		caseDict['params']['fields']['summary'] = data['summary']

		# 执行测试
		caseDict = self.__http_white_modify(caseDict)
		return caseDict

	def test_host_track_update(self, caseDict, setUp=False):
		'''关注host修改测试'''
		# setUp赋值和清理
		if 'setUp' in caseDict['params']:
			setUp = caseDict['params']['setUp']
			del caseDict['params']['setUp']
		# 判断是否需要准备修改的原数据
		if setUp:
			caseDictTemp = caseDict.copy()
			# 创建的数据备注信息为空
			caseDictTemp['params']['summary'] = ''
			self.test_host_track_add(caseDictTemp)
		# 查询需要修改的数据信息
		data = self.__search_data(caseDict)
		# 没有查询到数据，则将setUp置为True回调该函数
		if not data:
			caseDict = self.test_host_track_update(caseDict, True)
			return caseDict
		logging.debug('修改的原数据为：%s' % (data))
		caseDict['params']['op'] = 'update'
		caseDict['params']['ids'] = data['indexid']
		# 修改数据备注信息
		caseDict = self.__host_track_modify(caseDict)
		return caseDict

	def test_object_track_update(self, caseDict, setUp=False):
		'''关注对象修改测试'''
		# setUp赋值和清理
		if 'setUp' in caseDict['params']:
			setUp = caseDict['params']['setUp']
			del caseDict['params']['setUp']
		# 判断是否需要准备修改的原数据
		if setUp:
			caseDictTemp = caseDict.copy()
			# 创建的数据备注信息为空
			caseDictTemp['params']['summary'] = ''
			self.test_object_track_add(caseDictTemp)
		# 查询需要修改的数据信息
		data = self.__search_data(caseDict)
		# 没有查询到数据，则将setUp置为True回调该函数
		if not data:
			caseDict = self.test_object_track_update(caseDict, True)
			return caseDict
		logging.debug('修改的原数据为：%s' % (data))
		caseDict['params']['op'] = 'update'
		caseDict['params']['ids'] = data['indexid']
		# 修改数据备注信息
		caseDict = self.__object_track_modify(caseDict)
		return caseDict

	def test_url_track_update(self, caseDict, setUp=False):
		'''关注对象修改测试'''
		# setUp赋值和清理
		if 'setUp' in caseDict['params']:
			setUp = caseDict['params']['setUp']
			del caseDict['params']['setUp']
		# 判断是否需要准备修改的原数据
		if setUp:
			caseDictTemp = caseDict.copy()
			# 创建的数据备注信息为空
			caseDictTemp['params']['summary'] = ''
			self.test_url_track_add(caseDictTemp)
		# 查询需要修改的数据信息
		data = self.__search_data(caseDict)
		# 没有查询到数据，则将setUp置为True回调该函数
		if not data:
			caseDict = self.test_url_track_update(caseDict, True)
			return caseDict
		logging.debug('修改的原数据为：%s' % (data))
		caseDict['params']['op'] = 'update'
		caseDict['params']['ids'] = data['indexid']
		# 修改数据备注信息
		caseDict = self.__url_track_modify(caseDict)
		return caseDict

	def test_http_white_update(self, caseDict, setUp=False):
		'''关注对象修改测试'''
		# setUp赋值和清理
		if 'setUp' in caseDict['params']:
			setUp = caseDict['params']['setUp']
			del caseDict['params']['setUp']
		# 判断是否需要准备修改的原数据
		if setUp:
			caseDictTemp = caseDict.copy()
			# 创建的数据备注信息为空
			caseDictTemp['params']['summary'] = ''
			self.test_http_white_add(caseDictTemp)
		# 查询需要修改的数据信息
		data = self.__search_data(caseDict)
		# 没有查询到数据，则将setUp置为True回调该函数
		if not data:
			caseDict = self.test_http_white_update(caseDict, True)
			return caseDict
		logging.debug('修改的原数据为：%s' % (data))
		caseDict['params']['op'] = 'update'
		caseDict['params']['ids'] = data['indexid']
		# 修改数据备注信息
		caseDict = self.__http_white_modify(caseDict)
		return caseDict


if __name__ == '__main__':
	params = {'host': '192.168.60.186'}
	vc = http_data()
	vc.test_url_search(params)
